package com.demo.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wang
 */
public class HttpUtil {

  public static List<NameValuePair> convertMapToPair(Map<String,String> params) {
    List<NameValuePair> pairs = new ArrayList<>();
    for (Map.Entry<String,String> entry : params.entrySet()) {
      pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    return pairs;
  }

  public static String post(String url, Map<String,String> param) {
    String result = null;
    CloseableHttpResponse httpResponse = null;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    try {
      httpPost.setEntity(new UrlEncodedFormEntity(convertMapToPair(param), "utf-8"));
      httpResponse = httpClient.execute(httpPost);
      result = EntityUtils.toString(httpResponse.getEntity());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (httpResponse != null) {
          httpResponse.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (httpClient != null) {
          httpClient.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return result;
  }

  public static String postMulti(String url, Map<String,String> param, byte[] body) {
    String result = null;
    CloseableHttpResponse httpResponse = null;
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);

    MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
    reqEntity.addPart("content", new ByteArrayBody(body, ContentType.DEFAULT_BINARY, param.get("slice_id")));

    for (Map.Entry<String,String> entry : param.entrySet()) {
      StringBody value = new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8));
      reqEntity.addPart(entry.getKey(), value);
    }
    HttpEntity httpEntiy = reqEntity.build();

    try {
      httpPost.setEntity(httpEntiy);
      httpResponse = httpClient.execute(httpPost);
      result = EntityUtils.toString(httpResponse.getEntity());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (httpClient != null) {
          httpClient.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return result;
  }

  public static String postJson(String url, Map<String,String> params) {
    CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse httpResponse = null;
    try {
      HttpPost httpPost = new HttpPost(url);
      if (params != null) {
        StringEntity entity = new StringEntity(JSON.toJSONString(params), ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);
      }
      httpResponse = client.execute(httpPost);
      return EntityUtils.toString(httpResponse.getEntity(), Charset.defaultCharset());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (httpResponse != null) {
          httpResponse.close();
        }
        client.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
